#!/bin/bash
#
# Wrapper for xcrun that forces a custom device set for simctl commands

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REAL_XCRUN=$(PATH="${PATH//$SCRIPT_DIR:/}" command -v xcrun)

# If --set is specified respect that and pass through unchanged
for arg in "$@"; do
    if [[ "$arg" == "--set" ]]; then
        exec "$REAL_XCRUN" "$@"
    fi
done

# If the subcommand is 'simctl', inject --set <CUSTOM_DEVICE_SET> after 'simctl'
if [[ "$1" == "simctl" ]]; then
    exec "$REAL_XCRUN" "$1" --set "$CUSTOM_DEVICE_SET" "${@:2}"
else
    exec "$REAL_XCRUN" "$@"
fi